using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

namespace iMainAddin
{
    public class iDocument
    {
        DocumentEvents m_OpenDocument;
        TextEditorEvents m_TextEvent;
        TextDocument m_ActiveDocument;

        public static iDocument Create(DTE2 application)
        {
            return new iDocument(application);
        }

        public iDocument(DTE2 application)
        {
            m_OpenDocument = application.Events.get_DocumentEvents(null);
            m_OpenDocument.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(this.OpenDocument);
            m_TextEvent = application.Events.get_TextEditorEvents(null);
            m_TextEvent.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(this.DeleteBlank);
        }
        public EditPoint EndEditPoint
        {
            get
            {
                return m_ActiveDocument.EndPoint.CreateEditPoint();
            }
        }
        public EditPoint StartEditPoint
        {
            get
            {
                return m_ActiveDocument.StartPoint.CreateEditPoint();
            }
        }
        public void DeleteBlank( TextPoint StartPoint, TextPoint EndPoint, int Hint )
        {
            try
            {
                int numLines = EndPoint.Line - StartPoint.Line + 1;
                for (int i = 0; i < numLines; i++)
                {
                    EditPoint editPoint = StartPoint.CreateEditPoint();
                    String lineText = editPoint.GetLines(StartPoint.Line + i, StartPoint.Line + i + 1);
                    int deletePoint = lineText.Length-1;
                    int deleteCount = 0;
                    for (; deletePoint > 0; --deletePoint)
                    {
                        char now = lineText[deletePoint];
                        if (now == ' ' || now == '\t')
                        {
                            ++deleteCount;
                            continue;
                        }
                        else
                            break;
                    }
                    if(deleteCount != 0)
                    {
                        editPoint.MoveToLineAndOffset(StartPoint.Line + i, deletePoint + 2);
                        editPoint.Delete(deleteCount);
                    }                    
                }
            }
            catch (System.ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void OpenDocument( Document openDocument )
        {
            m_ActiveDocument = (TextDocument)openDocument;
        }
        public void AutoAddVariable(bool isSetFunc)
        {
            TextDocument textDouc = (TextDocument)m_ActiveDocument;
            //m_ActiveDocument.Selection.StartOfLine();
            EditPoint startPoint = StartEditPoint;
            String lineText = startPoint.GetText( EndEditPoint );
            String variableType = "";
            for ( int i = 0 ; i < startPoint.LineLength ; i++ )
            {
                char now = lineText[i];
                if (now == ' ' || now == '\t')
                {
                    variableType += now;
                }
                else
                {
                    variableType += now;
                }
            }

            String variableName = startPoint.GetText( EndEditPoint );
            m_ActiveDocument.Selection.NewLine(1);
            
            String getFunc = "public :\n";
            getFunc +=  variableType;
            getFunc += "get_";

            int underbarCount = variableName.CompareTo("_");
            variableName.Remove(0, underbarCount);
            getFunc += variableName;
           // m_ActiveDocument.Selection.Text = 
            //EditPoint editPoint = EndEditPoint;
        }
        public void CommandAddVariable(bool isSetFunc)
        {
            TextDocument textDouc = (TextDocument)m_ActiveDocument;
            EditPoint editPoint = EndEditPoint;

            //String getFunc = "get_";
            //int underbarCount = variableName.CompareTo("_");
            //variableName.Remove(0, underbarCount);
            //getFunc += variableName;
            //if( isSetFunc )
            //{
            //}
        }
    }
}
